///////////////////////////////////////////////////////////////////////////////
// Basic viewer extension
//
///////////////////////////////////////////////////////////////////////////////

BasicExtensionSample = function(viewer, options) {
  Bimsop.Viewing.Extension.call(this, viewer, options);

  var _self = this;
  var viewer = viewer;


  function rgbToHex(r, g, b) {
    return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
  }

  _self.highLightNodeByName = function (name) {
    var highLightColor = new THREE.Color(255, 0, 0);
    viewer.search(name,
      ["name"],
      function (results) {
        // 查询成功回调
        var result = results[0]; // 只有一个模型, 所以取results[0], result为所有名字中包含 name 的节点
        if (!result) {
          alert("查不到该设备编号：" + name);
          return;
        }

        for (var i = 0; i < result.length; i++) {
          var dbId = result[i].id;
          console.log(rgbToHex(255, 0, 0));
          //高亮展示
          //viewer.currentModel.setThemingColor(dbId, rgbToHex(255, 0, 0));
          viewer.currentModel.setThemingColor(dbId, highLightColor);
          viewer.isolate(dbId);
          //拉近视角
          viewer.fitToView(dbId);
          console.log("highLightNodeByName:" + dbId);
        }

        viewer.impl.invalidate(false, false, true);
      }, function (err) {
        // 错误处理
        console.error(err);
      })
  };


  _self.load = function() {
    console.log('BasicExtensionSample loaded');


    // _self.highLightNodeByName('DC3-C110-CHL-01');
    // _self.highLightNodeByName('DC3-T400-CT-07');
    _self.highLightNodeByName('DC3-C110-MCC4A-03');
    

    // console.log(viewer);
    // // there is only one model in this case
    // var currentModel = viewer.models[0];
    // currentModel.search(
    //   "垂直截面", //vaue
    //   "椽截面", //filed name
    //   function (result) {
    //     console.dir(result);

    //     console.log(result.length);

    //     var dbIds = [];

    //     for (let index = 0; index < result.length; index++) {
    //       // 这里拿到的结果中的id已经是dbId
    //       const dbId = result[index].id;
    //       console.log("----" + dbId);
    //       dbIds.push(dbId);
    //     }

    //     console.log(dbIds);

    //     viewer.fitToView(dbIds);

    //     viewer.isolate(dbIds);
    //   },
    //   function (err) {
    //     console.log(err);
    //   }
    // );


    return true;
  };

 

  _self.unload = function() {
    alert("BasicExtensionSample unloaded");

    return true;
  };




};

BasicExtensionSample.prototype = Object.create(
  Bimsop.Viewing.Extension.prototype
);

BasicExtensionSample.prototype.constructor = BasicExtensionSample;

Bimsop.Viewing.theExtensionManager.registerExtension(
  "BasicExtensionSample",
  BasicExtensionSample
);
